package cn.tedu.luban.demand.main.service;

import cn.tedu.luban.demand.main.repository.DemandRepository;
import cn.tedu.luban.demand.protocol.bo.RequestOrderBO;
import cn.tedu.luban.demand.protocol.param.GrabOrderParam;
import cn.tedu.luban.demand.protocol.query.RequestOrderQuery;
import com.tedu.inn.protocol.ListRecordTotalBO;
import java.math.BigDecimal;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DemandFrontService {
    @Autowired
    private DemandRepository demandRepository;
    public ListRecordTotalBO<RequestOrderBO> queryRequestOrder(RequestOrderQuery query) {
        Long total = demandRepository.getRequestOrderCount(query);
        List<RequestOrderBO> requestOrderBOS=null;
        if (total>0){
            //查询具体的列表
            //orderAmount 需求单原价 viewOrderAmount展示价格 是空 profitScale分润比例 也是null
            requestOrderBOS = demandRepository.queryRequestOrders(query);
            //为需求单列表中 展示金额做计算
            calculateViewOrderAmount(requestOrderBOS);
        }
        return new ListRecordTotalBO<>(requestOrderBOS,total);
    }
    private void calculateViewOrderAmount(List<RequestOrderBO> requestOrderBOS) {
        BigDecimal profitScale=new BigDecimal(20.00);
        for (RequestOrderBO requestOrderBO : requestOrderBOS) {
            Long orderAmount=requestOrderBO.getOrderAmount();
            BigDecimal workerPercent=new BigDecimal(100)
                .subtract(profitScale)
                .divide(new BigDecimal(100));
            log.info("计算出来的师傅分润百分比:{}",workerPercent);
            Long viewOrderAmount=new BigDecimal(orderAmount).multiply(workerPercent).longValue();
            requestOrderBO.setViewOrderAmount(viewOrderAmount);
            requestOrderBO.setProfitScale(profitScale);
            log.info("展示的师傅可收入价格:{}",viewOrderAmount);
        }
    }

    public Boolean grabOrder(String requestOrderNo) {
        //1.使用requestOrderNo 查询 version 数值
        RequestOrderBO requestOrderBO = demandRepository.getRequestOrder(requestOrderNo);
        //1.1 判断抢的需求单是否存在 存在 业务逻辑才继续
        if (requestOrderBO==null|| requestOrderBO.getId()==null){
            //因为没有 所以没抢到
            return false;
        }else{
            //2.调用仓储层 CAS 比较并交换
            //update request_order set grab_status=1 where #{request_order_no} and grab_status and #{version}
            GrabOrderParam grabOrderParam=new GrabOrderParam();
            grabOrderParam.setRequestOrderNO(requestOrderNo);
            grabOrderParam.setVersion(requestOrderBO.getVersion());
            //代码变动 GrabOrderParam从dao-api迁移到protocol requestOrderBO添加业务需要的version字段
            //仓储层 扩展一个抢单的方法 TODO
            return demandRepository.grabOrder(grabOrderParam)==1;
        }
    }

    public Boolean returnOrder(String requestOrderNo) {
        //1.使用requestOrderNo 查询 version 数值
        RequestOrderBO requestOrderBO=
            demandRepository.getRequestOrderGrabed(requestOrderNo);
        //1.1 判断归还的需求单是否存在 存在 业务逻辑才继续
        if (requestOrderBO==null|| requestOrderBO.getId()==null){
            //因为没有 没的还
            return false;
        }else{
            //2.调用仓储层 CAS 比较并交换
            //update request_order set grab_status=0 where #{request_order_no} and grab_status=1 and #{version}
            GrabOrderParam grabOrderParam=new GrabOrderParam();
            grabOrderParam.setRequestOrderNO(requestOrderNo);
            grabOrderParam.setVersion(requestOrderBO.getVersion());
            //代码变动 GrabOrderParam从dao-api迁移到protocol requestOrderBO添加业务需要的version字段
            return demandRepository.returnOrder(grabOrderParam)==1;
        }
    }
}
